package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.Customer;

public class CustomerPresenter extends AbstractPresenter<Customer>{
	final String SINGLEITEM = "customer";
	final String MULTIITEM = "customers";
	
	public Customer getEmptyEntity(){
		Customer entity = new Customer();
		return entity;
	}
	
	public ArrayList<Customer> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Customer tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}

	public Customer tryJSONParseNoHeader(String input){
		try{
			JSONObject jsonHop = new JSONObject(input);
			Customer item = createItemFromJSONObject(jsonHop);
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Customer createItemFromJSONObject(JSONObject jsonObject){
		Customer cust = new Customer();
		try{
			cust.setActive(jsonObject.getBoolean("CActive"));
			cust.setAddress(jsonObject.getString("CAddr"));
			cust.setCity(jsonObject.getString("CCity"));
			cust.setContact(jsonObject.getString("CContact"));
			cust.setEmail(jsonObject.getString("CEmail"));
			cust.setEmailCC(jsonObject.getString("CEmailCC"));
			cust.setId(jsonObject.getInt("CId"));
			cust.setName(jsonObject.getString("CName"));
			cust.setNotes(jsonObject.getString("CNotes"));
			cust.setPhone(jsonObject.getString("CPhone"));
			cust.setState(jsonObject.getString("CState"));
			cust.setTerms(jsonObject.getInt("CTerms"));
			cust.setZip(jsonObject.getInt("CZip"));
			cust.setDistCo(jsonObject.getBoolean("cdist"));
			cust.setOnHold(jsonObject.getInt("custHold"));
			if(!jsonObject.isNull("custGroupID")){
				//create product if set
				CustomerGroupPresenter cgPresenter = new CustomerGroupPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("custGroupID");
				cust.setGroup(cgPresenter.createItemFromJSONObject(jsonGroup));
			} else {
				cust.setGroup(null);
			}
			if(!jsonObject.isNull("discountCode")){
				//create product if set
				DiscountCodePresenter dcPresenter = new DiscountCodePresenter();
				JSONObject jsonDisc = jsonObject.getJSONObject("discountCode");
				cust.setDiscountCode(dcPresenter.createItemFromJSONObject(jsonDisc));
			} else {
				cust.setDiscountCode(null);
			}
			if(!jsonObject.isNull("custRouteID")){
				//create product if set
				RoutePresenter routePresenter = new RoutePresenter();
				JSONObject jsonRoute = jsonObject.getJSONObject("custRouteID");
				cust.setRoute(routePresenter.createItemFromJSONObject(jsonRoute));
			} else {
				cust.setRoute(null);
			}
			return cust;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Customer item){
		try{
			JSONObject json = new JSONObject();
			json.put("CActive", item.isActive());
			json.put("CAddr", item.getAddress());
			json.put("CCity", item.getCity());
			json.put("CContact", item.getContact());
			json.put("CEmail", item.getEmail());
			json.put("CEmailCC", item.getEmailCC());
			json.put("CId", item.getId());
			json.put("CName", item.getName());
			json.put("CNotes", item.getNotes());
			json.put("CPhone", item.getPhone());
			json.put("CState", item.getState());
			json.put("CTerms", item.getTerms());
			json.put("CZip", item.getZip());
			json.put("cdist", item.isDistCo());
			json.put("custHold", item.getOnHold());
			if(item.getGroup()!=null){
				CustomerGroupPresenter cgPresenter = new CustomerGroupPresenter();
				json.put("custGroupID", cgPresenter.createJSON(item.getGroup()));
			} else {
				json.put("custGroupID", JSONObject.NULL);
			}
			if(item.getDiscountCode()!=null){
				DiscountCodePresenter dcPresenter = new DiscountCodePresenter();
				json.put("discountCode", dcPresenter.createJSON(item.getDiscountCode()));
			} else {
				json.put("discountCode", JSONObject.NULL);
			}
			if(item.getRoute()!=null){
				RoutePresenter routePresenter = new RoutePresenter();
				json.put("custRouteID", routePresenter.createJSON(item.getRoute()));
			} else {
				json.put("custRouteID", JSONObject.NULL);
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
}
